﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;

namespace CodeRobot.Utility
{
    /// <summary>
    /// 日志记录类
    /// Notice: 调试时需要将App.Config文件拷贝到 Debug 目录下
    /// <example>For example:
    /// <code>
    /// try
    /// {
    ///     //doing something...
    /// }
    /// catch (Exception ex)
    /// {
    ///     MIM.Utility.LogHelper.Error(typeof(Forexbrokers), ex, "根据URL获取列表页面指定代码", "AccordingURLGetResultList");
    /// }
    /// </code>
    /// </example>
    /// </summary>
    public class LogHelper
    {

        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="t">通常是类名</param>
        /// <param name="ex">异常对象</param>
        /// <param name="strMessageObject">通常是方法名或指定记录消息类型</param>
        /// <param name="strFunctionName">通常是函数名</param>
        public static void Error(Type t, Exception ex,string strMessageObject,string strFunctionName)
        {
            string strPath = Application.StartupPath + "\\App.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));

            ILog ilog = LogManager.GetLogger(t);
            if(ilog.IsErrorEnabled)
            {
                ilog.Error(strMessageObject, ex);
            }
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="t">通常是类名</param>
        /// <param name="strMessageObject">通常是方法名或指定记录消息类型</param>
        /// <param name="strFunctionName">通常是函数名</param>
        public static void Debug(Type t, string strMessageObject, string strFunctionName)
        {
            string strPath = Application.StartupPath + "\\App.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));

            ILog ilog = LogManager.GetLogger(t);
            if (ilog.IsDebugEnabled)
            {
                ilog.Debug(strMessageObject);
            }
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="t">通常是类名</param>
        /// <param name="strMessageObject">通常是方法名或指定记录消息类型</param>
        /// <param name="strFunctionName">通常是函数名</param>
        public static void Info(Type t, string strMessageObject, string strFunctionName)
        {
            string strPath = Application.StartupPath + "\\App.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));

            ILog ilog = LogManager.GetLogger(t);
            if (ilog.IsInfoEnabled)
            {
                ilog.Info(strMessageObject);
            }
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="t">通常是类名</param>
        /// <param name="strMessageObject">通常是方法名或指定记录消息类型</param>
        /// <param name="strFunctionName">通常是函数名</param>
        public static void Warn(Type t, string strMessageObject, string strFunctionName)
        {
            string strPath = Application.StartupPath + "\\App.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));

            ILog ilog = LogManager.GetLogger(t);
            if (ilog.IsWarnEnabled)
            {
                ilog.Warn(strMessageObject);
            }
        }

        /// <summary>
        /// 记录严重错误日志
        /// </summary>
        /// <param name="t">通常是类名</param>
        /// <param name="strMessageObject">通常是方法名或指定记录消息类型</param>
        /// <param name="strFunctionName">通常是函数名</param>
        public static void Fatal(Type t, string strMessageObject, string strFunctionName)
        {
            string strPath = Application.StartupPath + "\\App.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));

            ILog ilog = LogManager.GetLogger(t);
            if (ilog.IsFatalEnabled)
            {
                ilog.Fatal(strMessageObject);
            }
        }

    }
}
